package com.loong.member.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.member.common.forehead.param.MemberAddressDetailQueryForeheadParam;
import com.loong.member.common.forehead.result.MemberAddressForeheadResult;
import com.loong.member.mapper.MemberAddressMapper;
import com.loong.member.model.MemberAddress;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2020/12/25 18:03
 */
@Repository
public class MemberAddressBusiness extends AbstractBusiness<MemberAddress> {

    @Resource
    private MemberAddressMapper addressMapper;

    /**
     * 根据参数查询会员地址详情
     * @param param 会员地址id和用户id封装的参数
     * @return 会员地址集合
     */
    public List<MemberAddress> findMemberAddressDetailByParam(MemberAddressDetailQueryForeheadParam param){
        Condition condition = new Condition(MemberAddress.class);
        Example.Criteria criteria = condition.createCriteria();
        // 会员地址id
        criteria.andEqualTo("id",param.getAddressId());
        // 用户id
        criteria.andEqualTo("userId",param.getUserId());
        return findByCondition(condition);
    }

    /**
     * 根据用户id查询用户的默认地址
     * @param userId 用户id
     * @return 会员地址集合
     */
    public List<MemberAddress> findDefaultMemberAddressByUserId(Long userId){
        Condition condition = new Condition(MemberAddress.class);
        Example.Criteria criteria = condition.createCriteria();
        //默认地址
        criteria.andEqualTo("defaultYes",true);
        // 用户id
        criteria.andEqualTo("userId",userId);
        return findByCondition(condition);
    }

    /**
     * 根据用户id设置用户所有收货地址不是默认
     * @param userId 用户id
     */
    public void updateMemberAddressNotDefaultByUserId(Long userId){
        addressMapper.updateAllAddressNotDefaultByUserId(userId);
    }
}
